package com.lam.system.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lam.common.utils.StringUtils;
import com.lam.common.validate.ValidateUtil;
import com.lam.system.entity.SysMenu;
import com.lam.system.mapper.SysMenuMapper;
import com.lam.system.param.SysMenuParam;
import com.lam.system.service.SysMenuService;

/**
 * 菜单管理Service业务层处理
 * 
 * @author admin
 * @date 2022-09-01
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    /**
     * 查询菜单管理
     * 
     * @param id ID
     * @return 单个对象
     */
    @Override
    @Transactional(readOnly = true)
    public SysMenu selectById(String id) {

        return sysMenuMapper.selectById(id);
    }
    
    @Override
	public List<SysMenu> selectMenuByUser(String uid) {
		
		return sysMenuMapper.selectMenuByUser(uid);
	}

    /**
     * 查询菜单管理列表
     * 
     * @param param
     * @return 
     */
    @Override
    @Transactional(readOnly = true)
    public List<SysMenu> selectList(SysMenuParam param) {
    
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        
        //关键字查询
        if(StringUtils.isNotBlank(param.getKeywords())) {
        	queryWrapper.and(wr->{
        		wr.or().like("name", param.getKeywords());
        		wr.or().like("description", param.getKeywords());
        		wr.or().like("path", param.getKeywords());
        		wr.or().like("component", param.getKeywords());
        		wr.or().like("perms", param.getKeywords());
        	});
        }
        
        queryWrapper.eq(Objects.nonNull(param.getParentId()), "parent_id", param.getParentId());
        queryWrapper.like(StringUtils.isNotBlank(param.getNameKey()), "name", param.getNameKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getName()), "name", param.getName());
        queryWrapper.eq(StringUtils.isNotBlank(param.getDescription()), "description", param.getDescription());
        queryWrapper.eq(Objects.nonNull(param.getStatus()), "status", param.getStatus());
        queryWrapper.ne(StringUtils.isNotBlank(param.getNeId()), "id", param.getNeId());
        if(StringUtils.isNotBlank(param.getMenuType())) {
        	if(param.getMenuType().contains(",")) {
        		queryWrapper.in("menu_type", Arrays.asList(param.getMenuType().split(",")));
        	}else {
        		queryWrapper.eq("menu_type", param.getMenuType());
        	}
        }

        return sysMenuMapper.selectList(queryWrapper);
    }

    /**
     * 新增菜单管理
     * 
     * @param sysMenu 
     * @return 
     */
    @Override
    @Transactional
    public int insert(SysMenu sysMenu) {
    
        return sysMenuMapper.insert(sysMenu);
    }

    /**
     * 修改菜单管理
     * 
     * @param sysMenu 
     * @return 
     */
    @Override
    @Transactional
    public int update(SysMenu sysMenu) {
    
        return sysMenuMapper.updateById(sysMenu);
    }

    /**
     * 删除菜单管理信息
     * 
     * @param idStr ID，多个用逗号分隔
     * @return 
     */
    @Override
    @Transactional
    public int deleteById(String idStr) {
    	if(StringUtils.isBlank(idStr)) {
            return 0;
    	}
    	
    	ValidateUtil.validDataUsed("sys_menu", "parent_id", idStr, "菜单有子节点，不可删除");
    	ValidateUtil.validDataUsed("sys_role_menu", "menu_id", idStr, "菜单有关联角色数据，不可删除");
    	
        return sysMenuMapper.deleteBatchIds(StringUtils.str2List(idStr));
    }

}
